<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="appKey">
              <el-input v-model="filters.appKey" clearable placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="thirdName">
              <el-input v-model="filters.thirdName" clearable placeholder="第三方名称"></el-input>
            </el-form-item>
            <el-form-item prop="thirdType">
              <el-select v-model="filters.thirdType" clearable placeholder="第三方类型">
                <el-option v-for="item in thirdTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="state">
              <el-select v-model="filters.tokenStatus" clearable placeholder="授权状态">
                <el-option v-for="item in tokenStatues" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <!--<el-form-item prop="state">
              <el-select v-model="filters.state" clearable placeholder="状态">
                <el-option
                  v-for="item in dataStatue"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>-->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <!-- 列表 -->
      <el-table :data="dataList" :fit="true" highlight-current-row @row-dblclick="handleListDate">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="thirdName" label="第三方名称"></el-table-column>
        <el-table-column prop="appKey" label="用户名"></el-table-column>
        <el-table-column prop="appSecret" label="密码" width="180"></el-table-column>
        <el-table-column prop="accessToken" label="访问令牌"></el-table-column>
        <el-table-column prop="accessExpires" label="令牌过期时间"></el-table-column>
        <el-table-column prop="hospitalName" label="医院"></el-table-column>
        <el-table-column prop="institutionName" label="医检"></el-table-column>
        <el-table-column prop="linkMan" label="联系人"></el-table-column>
        <el-table-column prop="linkPhone" label="联系人电话"></el-table-column>
        <el-table-column prop="linkAddr" label="联系人地址"></el-table-column>
        <el-table-column label="授权状态" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatTokenStates(scope.row.tokenStatus)">
                {{ formatTokenState(scope.row.tokenStatus) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <!--<el-table-column prop="state" label="状态" :formatter="formatState" width="100"></el-table-column>-->
        <el-table-column prop="createDate" label="创建时间" width="180"></el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" size="mini">编辑</el-button>
            <!--<el-button v-has="permissionUpStatus" @click="handleIsDisable(scope.row)" size="mini">{{scope.row.state == 0? '启用' : '禁用'}}</el-button>-->
            <el-button v-has="permissionDelete" @click="handleClickDelete(scope.row)" size="mini">删除</el-button>
            <!-- <el-button v-has="permissionEdit" @click="handleListDate(scope.row)" size="mini">查看账号列表</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页工具条 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';
import OrganApi from '@/api/organ/organ';

export default {
  name: 'ThirdAuthManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    let _this = this;
    return {
      dataList: [],
      filters: {
        appKey: '',
        thirdName: '',
        thirdType: null,
        tokenStatus: null,
        state: null,
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      dataStatue: [
        { label: '禁用', value: '0' },
        { label: '启用', value: '1' },
      ],
      //授权状态 0.未授权 1.已授权 2.授权失效
      tokenStatues: [
        { label: '未授权', value: '0' },
        { label: '已授权', value: '1' },
        { label: '授权失效', value: '2' },
      ],
      thirdTypeList: [
        { label: '医院', value: '1' },
        { label: '医检', value: '2' },
        { label: '其他', value: '10' },
      ],

      permissionAdd: 'SYSTEM_MANAGER_ORGAN_THIRD_AUTH_LIST_ADD',
      permissionEdit: 'SYSTEM_MANAGER_ORGAN_THIRD_AUTH_LIST_EDIT',
      permissionUpStatus: 'SYSTEM_MANAGER_ORGAN_THIRD_AUTH_LIST_STATE',
      permissionDelete: 'SYSTEM_MANAGER_ORGAN_THIRD_AUTH_LIST_DELETE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    //授权状态 0.未授权 1.已授权 2.授权失效
    formatTokenState(cellValue) {
      if (cellValue == 0) {
        return '未授权';
      } else if (cellValue == 1) {
        return '已授权';
      } else if (cellValue == 2) {
        return '授权失效';
      }
    },
    formatTokenStates(cellValue) {
      if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      } else if (cellValue == 2) {
        return 'warning';
      }
    },
    formatState(row, column, cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      } else if (cellValue == 2) {
        return '待审核';
      }
    },
    formatDate(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, FORMAT_DATE_YYYY_MM_DD);
    },
    formatDateTime(row, column, cellValue) {
      let _this = this;
      return _this.$Utils.formatDate(cellValue, 'yyyy-MM-dd hh:mm:ss');
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    handleClickAdd() {
      let _this = this;
      _this.$router.push('/organ/thirdAuthAdd');
    },
    handleClickEditor(row) {
      let _this = this;
      _this.$router.push({
        path: '/organ/thirdAuthEdit',
        query: { id: row.thirdId },
      });
    },
    /* handleClickDetail (row) {
        let _this = this
        _this.$router.push({
          path: '/sys/thirdAuthDetail',
          query: { id: row.thirdId }
        })
      },*/
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state == 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        OrganApi.deleteThirdAuth({
          thirdId: row.thirdId,
          state: currentState,
        }).then((res) => {
          _this.listData();
        });
      });
    },
    handleClickDelete(val) {
      let _this = this;
      _this.$confirm(message.MSG_CONFIRM_DELETE, message.TITLE_PROMPT, {}).then(() => {
        OrganApi.deleteThirdAuth({
          thirdId: val.thirdId,
          state: -1,
        }).then((res) => {
          _this.listData();
          this.$Utils.notifySuccess();
        });
      });
    },
    // 根据ID查看对应账号列表
    handleListDate(row, event, column) {
      let _this = this;
      if (row.thirdId == 3) {
        // 满足条件，让它跳转到迪安账号列表
        // 这边还需要传一个ID
        _this.$router.push({ path: '/organ/thirdAccountManagement' });
      } else {
        this.$message({
          type: 'error',
          message: '未找到对应账号列表请重试',
        });
      }
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      let cond = {
        pageNo: _this.page.pageNo,
        thirdType: _this.filters.thirdType,
        tokenStatus: _this.filters.tokenStatus,
        pageSize: _this.page.pageSize,
      };
      let thirdName = _this.filters.thirdName;
      let appKey = _this.filters.appKey;
      let state = _this.filters.state;
      if (thirdName !== undefined && thirdName !== null && thirdName !== '') {
        cond.thirdName = thirdName;
      }
      if (appKey !== undefined && appKey !== null && appKey !== '') {
        cond.appKey = appKey;
      }
      if (state !== undefined && state !== null && state !== '') {
        cond.state = state;
      }

      OrganApi.listThirdAuth(cond).then((res) => {
        _this.dataList = res.data.list;
        _this.page.pageTotal = res.data.total;
      });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
